package tech.wetech.admin.modules.system.web;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin.core.annotation.SystemLog;
import tech.wetech.admin.core.utils.Result;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.web.BaseCrudController;
import tech.wetech.admin.modules.system.po.ProjectManagement;
import tech.wetech.admin.modules.system.service.ProjectManagementService;

import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author lu
 * @description 项目负责信息管理的控制层
 * @date 2019年07月29日 15:09
 */
@Controller
@RequestMapping("/projectmanagement")
public class ProjectManagementController extends BaseCrudController<ProjectManagement> {

    @Autowired
    private ProjectManagementService projectManagementService;

    @ModelAttribute("searchreservation1")//
    public String searchreservation1() {
        //获取服务器事件转成合适的格式
        Date currentTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,1);
        Date time = calendar.getTime();
        //改变输出格式（自己想要的格式）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String streStartTime = formatter.format(currentTime);
        String streStartTime1 = formatter.format(time);
        return streStartTime+" ~ "+streStartTime1;
    }

    @ModelAttribute("searchstreStartTime")//
    public String searchstreStartTime() {
        //获取服务器事件转成合适的格式
        Date currentTime = new Date();
        //改变输出格式（自己想要的格式）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String streStartTime = formatter.format(currentTime);
        return streStartTime;
    }

    @ModelAttribute("searchstreEndTime")//
    public String searchstreEndTime() {


        //获取服务器事件转成合适的格式
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,1);
        Date time = calendar.getTime();
        //改变输出格式（自己想要的格式）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String streStartTime1 = formatter.format(time);
        return streStartTime1;
    }

    @GetMapping
    public String userPage(Model model) {
        return "system/projectmanagement";
    }


    @GetMapping("/selectlist")
    @ApiOperation("分页查询数据")
    @ResponseBody
    public Result selectlist(ProjectManagement projectManagement, PageQuery pageQuery) {
        List<ProjectManagement> list = projectManagementService.selectList(projectManagement, pageQuery);
        return Result.success(list)
                .addExtraIfTrue(pageQuery.isCountSql(), "total", ((Page) list).getTotal());
    }

    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("projectmanagement:create")
    @SystemLog("项目负责信息新增")
    @Override
    public Result create(ProjectManagement projectManagement) {
        projectManagementService.createProjectManagement(projectManagement);
        return Result.success();
    }

    @ResponseBody
    @SystemLog("项目负责信息修改")
    @RequiresPermissions("projectmanagement:update")
    @PostMapping("/update")
    @Override
    public Result update(@Validated ProjectManagement projectManagement) {
        projectManagementService.update(projectManagement);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("projectmanagement:delete")
    @SystemLog("项目负责信息删除")
    @PostMapping("/delete-batch")
    @Override
    public Result deleteBatchByIds(@NotNull @RequestParam("id") Object[] ids) {
        super.deleteBatchByIds(ids);
        return Result.success();
    }
}
